﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace PTCScheduler.FileMenu_Pages
{
    /// <summary>
    /// Interaction logic for Add_CounselorPage.xaml
    /// </summary>
    public partial class Add_CounselorPage : Page
    {
        public Add_CounselorPage()
        {
            InitializeComponent();
        }

        //Method to hide all labels.
        public void HideAllLabels()
        {
            //Visibility.Hidden.
            lblCounselorExists.Visibility = Visibility.Hidden;
            lblCounselorSuccess.Visibility = Visibility.Hidden;
            lblValidEmail.Visibility = Visibility.Hidden;
            lblProvideAllInfo.Visibility = Visibility.Hidden;
            lblValidName.Visibility = Visibility.Hidden;
            //Clear datagrid.
            dataGridCounselor.ItemsSource = null;
        }

        //Clear the form and refocus to first name textbox.
        public void ClearForm()
        {
            //Clear textboxes.
            txtCounselorFName.Clear();
            txtCounselorLName.Clear();
            txtCounselorEmail.Clear();
            //Refocus.
            txtCounselorFName.Focus();
        }

        //Method to check for valid email.
        public Boolean validEmail(String email)
        {
            if (email.Contains("@") && email.Contains("."))
            {
                return true;
            }
            return false;
        }

        //Handles the submit button click.
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            //Hide all labels.
            HideAllLabels();

            try
            {
                //Check to ensure Counselor first name and last name is entered.
                if (txtCounselorFName.Text == String.Empty || txtCounselorLName.Text == String.Empty)
                {
                    throw new MissingFieldException();
                }
                //Check to ensure the counselor's name contains no numbers
                if (!Regex.IsMatch(txtCounselorFName.Text, @"^[a-zA-Z]+$") || !Regex.IsMatch(txtCounselorFName.Text, @"^[a-zA-Z]+$"))
                {
                    throw new ArgumentException();
                }

                //Create new Counselor object
                Counselor Counselor = new Counselor(txtCounselorFName.Text, txtCounselorLName.Text, txtCounselorEmail.Text);

                //Check for valid email and throw exception if not valid.
                if (!validEmail(txtCounselorEmail.Text))
                {
                    throw new FormatException();
                }
                else//If valid add Counselor.
                {
                    //Try catch to ensure no existing Counselor is added. Catches SQLite exception.
                    try
                    {
                        //Add New Counselor.
                        Counselor.AddNewCounselor();

                        //Display Counselor in datagrid.
                        dataGridCounselor.ItemsSource = Counselor.dvCounselor();

                        //Prompt user Counselor was added successfully.
                        lblCounselorSuccess.Visibility = Visibility.Visible;

                        //Clear the form.
                        ClearForm();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        //Display Counselor exists label.
                        lblCounselorExists.Visibility = Visibility.Visible;

                        //Display existing Counselor in datagrid.
                        dataGridCounselor.ItemsSource = Counselor.dvCounselor();

                        //Clear form.
                        ClearForm();
                    }//End try catch to catch sqlite exception
                }
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    //Display invalid email label.
                    lblValidEmail.Visibility = Visibility.Visible;
                }
                else if (ex is MissingFieldException)
                {
                    //Display input all information label.
                    lblProvideAllInfo.Visibility = Visibility.Visible;
                }
                else if (ex is ArgumentException)
                {
                    lblValidName.Visibility = Visibility.Visible;
                }
            }
        }


    }
}
